package de.uhlesoluschns.opensearch.provider;

import org.json.JSONArray;
import org.json.JSONException;

import android.database.AbstractCursor;
import de.uhlesoluschns.opensearch.OpenSearch.Suggestion;

public class SuggestionsCursor extends AbstractCursor {
	
	public static final String [] COLUMNS = {Suggestion.COL_ID, Suggestion.COL_SUGGESTION};
	
	private JSONArray suggestions;
	
	public SuggestionsCursor(JSONArray suggestions) {
		this.suggestions = suggestions;
	}

	@Override
	public String[] getColumnNames() {
		return COLUMNS;
	}

	@Override
	public int getCount() {
		return suggestions.length();
	}

	@Override
	public double getDouble(int column) {
		throw new UnsupportedOperationException();
	}

	@Override
	public float getFloat(int column) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int getInt(int column) {
		if(column == 0) {
			return mPos;
		}
		throw new UnsupportedOperationException();
	}

	@Override
	public long getLong(int column) {
		if(column == 0) {
			return mPos;
		}
		throw new UnsupportedOperationException();
	}

	@Override
	public short getShort(int column) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getString(int column) {
		if(mPos != -1 && column == 1) {
			try {
				return suggestions.getString(mPos);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean isNull(int column) {
		throw new UnsupportedOperationException();
	}

}
